package o;

import android.content.Context;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.huawei.appgallery.packagemanager.api.bean.ManagerTask;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import o.cof.a;

public class cpa {
    private static final cpa ˎ = new cpa();
    private Map<String, ManagerTask> ˊ = new ConcurrentHashMap();
    private ManagerTask ˋ = null;
    private cow ॱ = new cow();

    public static cpa ˊ() {
        return ˎ;
    }

    private cpa() {
    }

    @Nullable
    public ManagerTask ˏ(String str, cod... o_codArr) {
        if (!(this.ˋ == null || str == null || !str.equals(this.ˋ.packageName))) {
            for (cod o_cod : o_codArr) {
                if (this.ˋ.processType == o_cod) {
                    return this.ˋ;
                }
            }
        }
        return null;
    }

    public void ˏ(Context context, ManagerTask managerTask) {
        this.ॱ.ˎ(managerTask);
        cpc.ˎ(context).ˊ(managerTask);
    }

    public int ˋ(Context context, String str, long j, boolean z) {
        ManagerTask ˊ = this.ॱ.ˊ(str, j);
        if (ˊ == null) {
            return -1;
        }
        this.ॱ.remove((Object) ˊ);
        cpc ˎ = cpc.ˎ(context);
        ˎ.ॱ();
        ˎ.ˎ(j);
        ˎ.ˏ();
        if (z) {
            cpq.ˋ(ˊ);
        }
        return 1;
    }

    public ManagerTask ॱ(long j) {
        Iterator it = this.ॱ.iterator();
        while (it.hasNext()) {
            ManagerTask managerTask = (ManagerTask) it.next();
            if (j == managerTask.taskId) {
                return managerTask;
            }
        }
        return null;
    }

    public ManagerTask ˎ(long j) {
        if (this.ˋ == null || this.ˋ.taskId != j) {
            return null;
        }
        return this.ˋ;
    }

    @Nullable
    public ManagerTask ˏ(String str) {
        ManagerTask managerTask = null;
        if (!(str == null || this.ˋ == null || !str.equals(this.ˋ.packageName))) {
            managerTask = this.ˋ;
        }
        if (managerTask != null) {
            return managerTask;
        }
        List ˎ = this.ॱ.ˎ(str);
        if (ˎ != null) {
            managerTask = ˏ(ˎ);
        }
        if (managerTask == null) {
            return (ManagerTask) this.ˊ.get(str);
        }
        return managerTask;
    }

    private ManagerTask ˏ(@NonNull List<ManagerTask> list) {
        Collections.sort(list, cop.ˎ());
        for (ManagerTask managerTask : list) {
            if (managerTask.processType == cod.UNINSTALL) {
                if (managerTask.mode == 1001) {
                    return managerTask;
                }
            } else if (managerTask.mode == 1) {
                return managerTask;
            }
        }
        return null;
    }

    public int ॱ() {
        return this.ॱ.size();
    }

    @Nullable
    public ManagerTask ˋ() {
        Collections.sort(this.ॱ, cop.ˎ());
        if (this.ॱ.isEmpty()) {
            return null;
        }
        return this.ॱ.ˋ(0);
    }

    public void ॱ(ManagerTask managerTask) {
        this.ˋ = managerTask;
    }

    public void ˎ() {
        this.ˋ = null;
    }

    public void ˋ(@NonNull List<ManagerTask> list) {
        this.ˊ.clear();
        for (ManagerTask managerTask : list) {
            ManagerTask managerTask2 = (ManagerTask) this.ˊ.get(managerTask.packageName);
            if (managerTask2 != null) {
                if (managerTask2.versionCode < managerTask.versionCode) {
                    cpq.ˋ(managerTask2);
                    cnu.ˏ.ˏ("UninstalledTask", managerTask2.packageName + " found last older version:" + managerTask2.versionCode);
                } else {
                    cpq.ˋ(managerTask);
                    cnu.ˏ.ˏ("UninstalledTask", managerTask.packageName + " found current older version:" + managerTask.versionCode);
                }
            }
            this.ˊ.put(managerTask.packageName, managerTask);
        }
    }

    @Nullable
    public ManagerTask ˊ(@NonNull String str) {
        return (ManagerTask) this.ˊ.get(str);
    }

    @NonNull
    public List<ManagerTask> ˏ() {
        return new ArrayList(this.ˊ.values());
    }

    public void ˎ(@NonNull Context context, String str) {
        cqa ˏ = cqa.ˏ(context);
        ˏ.ॱ();
        ˏ.ˋ(str);
        this.ˊ.remove(str);
        ˏ.ˏ();
    }

    public void ॱ(Context context) {
        cqa ˏ = cqa.ˏ(context);
        ˏ.ॱ();
        ˏ.ˎ();
        this.ˊ.clear();
        ˏ.ˏ();
    }

    public void ˋ(@NonNull Context context, @NonNull ManagerTask managerTask) {
        if (TextUtils.isEmpty(managerTask.packageName)) {
            cnu.ˏ.ˊ("PackageTaskManager", "invalid task, packageName is empty.");
        } else if (managerTask.apkInfos == null || managerTask.apkInfos.isEmpty()) {
            cnu.ˏ.ˊ("PackageTaskManager", "invalid task, apkInfos is empty.");
        } else {
            Object obj = ((a) managerTask.apkInfos.get(0)).ˎ;
            if (TextUtils.isEmpty(obj)) {
                cnu.ˏ.ˊ("PackageTaskManager", "invalid task, file is empty.");
                return;
            }
            ManagerTask ॱ = ManagerTask.ॱ(managerTask);
            if (new File(obj).getName().startsWith("predl_")) {
                ॱ.status = cnz.UNINSTALLED_PREDL;
            } else {
                ॱ.status = cnz.UNINSTALLED;
            }
            if (this.ˊ.containsKey(ॱ.packageName)) {
                cqa.ˏ(context).ˋ(ॱ.packageName);
            }
            cqa.ˏ(context).ˎ(ॱ);
            this.ˊ.put(ॱ.packageName, ॱ);
        }
    }
}
